package com.future.message.pojo;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.bytebuddy.asm.Advice;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @description:
 * @projectName:message
 * @see:com.future.message.pojo
 * @author:Xuyujie
 * @createTime:2021/12/6 9:01
 * @version:1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SecurityUser implements UserDetails {

    //当前登录的用户
    private transient User currentUserInfo;

    //当前用户的权限
    private List<String> permissionValueList;

    public User getCurrentUserInfo() {
        return currentUserInfo;
    }

    public SecurityUser(User currentUserInfo) {
        if(currentUserInfo != null){
            this.currentUserInfo = currentUserInfo;
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> athorities = new ArrayList<>();
        for (String permissionValue:
             permissionValueList) {
            if(StringUtils.hasLength(permissionValue)){
                athorities.add(new SimpleGrantedAuthority("ROLE_"+permissionValue));
            }
        }
        return athorities;
    }

    @Override
    public String getPassword() {
        return currentUserInfo.getPassword();
    }

    @Override
    public String getUsername() {
        return currentUserInfo.getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
